﻿using System;
using System.Data;
using System.ServiceModel;
using Cinchoo.Core.Xml.Serialization;
using System.Reflection;
using Cinchoo.Core;
using Cinchoo.Core.Diagnostics;
using System.Data.Common;
using System.Threading;
using System.Collections.Generic;
using Cinchoo.Core.ETL;
using System.Linq;

namespace ChoWorkflowServer.Model
{
    public abstract class ChoSynchronizedDataModelImpl : ChoDataModelImpl
    {
        #region Instance Data Members (Private)
        
        private readonly object _syncRoot = new object();

        #endregion Instance Data Members (Private)

        public override int GetTaskId(string taskName)
        {
            lock (_syncRoot)
            {
                return base.GetTaskId(taskName);
            }
        }
        public override bool IsTaskExists(string taskName)
        {
            lock (_syncRoot)
            {
                return base.IsTaskExists(taskName);
            }
        }

        public override int AddOrUpdateTask(string taskName, string workflowName, string taskDesc, int taskGroupId, string cmdParameters,
        int? maxNoOfRetry, int? sleepBetweenRetry, int? timeout, string hostName,
        ChoTasksDataSet.CHO_TASK_SCHEDULESDataTable taskScheduleInfo, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                return base.AddOrUpdateTask(taskName, workflowName, taskDesc, taskGroupId, cmdParameters,
                    maxNoOfRetry, sleepBetweenRetry, timeout, hostName, taskScheduleInfo, modifiedBy, modifiedByHostName);
            }
        }

        public override void MoveTask(string taskName, int srcTaskGrpId, int destTaskGrpId, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.MoveTask(taskName, srcTaskGrpId, destTaskGrpId, modifiedBy, modifiedByHostName);
            }
        }

        public override int CloneTask(string taskName, string newTaskName, string taskGroupName, string newTaskGroupName, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                return base.CloneTask(taskName, newTaskName, taskGroupName, newTaskGroupName, modifiedBy, modifiedByHostName);
            }
        }

        public override void DeleteTask(string taskName, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.DeleteTask(taskName, modifiedBy, modifiedByHostName);
            }
        }

        public override ChoTasksDataSet.CHO_TASKSDataTable GetAllUITasks(string taskName, string taskGrpName)
        {
            lock (_syncRoot)
            {
                return base.GetAllUITasks(taskName, taskGrpName);
            }
        }

        public override ChoTasksDataSet.CHO_TASKSDataTable GetAllTasks(string taskName, string taskGrpName, string hostName, DateTime modifiedDate)
        {
            lock (_syncRoot)
            {
                return base.GetAllTasks(taskName, taskGrpName, hostName, modifiedDate);
            }
        }

        public override int AddOrUpdateTaskGroup(string taskGroupName, string taskGroupDesc, int parent, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                return base.AddOrUpdateTaskGroup(taskGroupName, taskGroupDesc, parent, modifiedBy, modifiedByHostName);
            }
        }

        public override void DeleteTaskGroup(string taskGroupName, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.DeleteTaskGroup(taskGroupName, modifiedBy, modifiedByHostName);
            }
        }

        public override bool IsTaskGroupExists(string taskGroupName)
        {
            lock (_syncRoot)
            {
                return base.IsTaskGroupExists(taskGroupName);
            }
        }

        public override ChoTasksDataSet.CHO_SCHEDULE_TYPESDataTable GetTaskScheduleTypes()
        {
            lock (_syncRoot)
            {
                return base.GetTaskScheduleTypes();
            }
        }

        public override ChoTasksDataSet.CHO_TASK_SCHEDULESDataTable GetTaskSchedules(int taskId, string scheduleName)
        {
            lock (_syncRoot)
            {
                return base.GetTaskSchedules(taskId, scheduleName);
            }
        }

        public override void SetTaskStatus(int batchId, string taskName, string scheduleName, Cinchoo.Core.ETL.ChoETLWorkflowStatus taskStatus, string modifiedBy, string modifiedByHostName, string logMsg = null, string nextRunTime = null, string machineName = null)
        {
            lock (_syncRoot)
            {
                base.SetTaskStatus(batchId, taskName, scheduleName, taskStatus, modifiedBy, modifiedByHostName, logMsg, nextRunTime, machineName);
            }
        }

        public override Cinchoo.Core.ETL.ChoETLWorkflowStatus GetTaskStatus(string taskName, string scheduleName)
        {
            lock (_syncRoot)
            {
                return base.GetTaskStatus(taskName, scheduleName);
            }
        }

        public override int GetTaskExitCode(string taskName, string scheduleName)
        {
            lock (_syncRoot)
            {
                return base.GetTaskExitCode(taskName, scheduleName);
            }
        }

        public override void AddOrUpdateTaskBatch(int batchId, string taskName, string scheduleName, int taskStatus, string cmdParameters, string machineName, string logMsg, DateTime processStart,
            DateTime? processEnd, TimeSpan? duration, string nextRunTime, int exitCode, int? retryCount, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.AddOrUpdateTaskBatch(batchId, taskName, scheduleName, taskStatus, cmdParameters, machineName, logMsg, processStart,
                    processEnd, duration, nextRunTime, exitCode, retryCount, modifiedBy, modifiedByHostName);
            }
        }

        public override ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable GetAllHolidayCalendars()
        {
            lock (_syncRoot)
            {
                return base.GetAllHolidayCalendars();
            }
        }

        public override void UpdateHolidayCalendar(ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable calendarTable,
            ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable calendarDatesTable, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.UpdateHolidayCalendar(calendarTable, calendarDatesTable, modifiedBy, modifiedByHostName);
            }
        }

        public override ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable GetAllHolidayCalendarDatesById(int holidayCalId)
        {
            lock (_syncRoot)
            {
                return base.GetAllHolidayCalendarDatesById(holidayCalId);
            }
        }

        public override ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable GetAllHolidayCalendarDatesByName(string holidayCalName)
        {
            lock (_syncRoot)
            {
                return base.GetAllHolidayCalendarDatesByName(holidayCalName);
            }
        }

        public override int CloneCalendar(string calName, string newCalName, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                return base.CloneCalendar(calName, newCalName, modifiedBy, modifiedByHostName);
            }
        }

        public override ChoTasksDataSet.CHO_POLICYDataTable GetAllPolicy()
        {
            lock (_syncRoot)
            {
                return base.GetAllPolicy();
            }
        }

        public override ChoTasksDataSet.CHO_USERSDataTable GetAllUsers()
        {
            lock (_syncRoot)
            {
                return base.GetAllUsers();
            }
        }

        public override void UpdateUsers(ChoTasksDataSet.CHO_USERSDataTable usersTable, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.UpdateUsers(usersTable, modifiedBy, modifiedByHostName);
            }
        }

        public override ChoTasksDataSet.CHO_ROLESDataTable GetAllRoles()
        {
            lock (_syncRoot)
            {
                return base.GetAllRoles();
            }
        }

        public override ChoTasksDataSet.CHO_ROLE_USERSDataTable GetAllUserRoles(int roleId)
        {
            lock (_syncRoot)
            {
                return base.GetAllUserRoles(roleId);
            }
        }

        public override ChoTasksDataSet.CHO_ROLE_POLICYDataTable GetAllRolePolicy(int roleId)
        {
            lock (_syncRoot)
            {
                return base.GetAllRolePolicy(roleId);
            }
        }

        public override void UpdateRoles(ChoTasksDataSet.CHO_ROLESDataTable rolesTable, ChoTasksDataSet.CHO_ROLE_USERSDataTable userRolesTable,
            ChoTasksDataSet.CHO_ROLE_POLICYDataTable rolesPolicyTable, string modifiedBy, string modifiedByHostName)
        {
            lock (_syncRoot)
            {
                base.UpdateRoles(rolesTable, userRolesTable, rolesPolicyTable, modifiedBy, modifiedByHostName);
            }
        }

        public override ChoTasksDataSet.CHO_POLICYDataTable GetPolicyForUser(string userName)
        {
            lock (_syncRoot)
            {
                return base.GetPolicyForUser(userName);
            }
        }

        public override ChoTasksDataSet.CHO_DEPENDENCY_TASKSDataTable GetAllDepedencyTasks(string jobName)
        {
            lock (_syncRoot)
            {
                return base.GetAllDepedencyTasks(jobName);
            }
        }
    }
}
